/*
 * @Author: your name
 * @Date: 2021-05-26 17:15:04
 * @LastEditTime: 2021-05-28 14:38:09
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bazai_music_api\controller\playlist.js
 */
const { default: axios } = require('axios')
const Router = require('koa-router')
const router = new Router()
const callCloudFn = require('../utils/callCloudFn')
const callCloudDB = require('../utils/callCloudDB')


router.get('/list', async (ctx, next) => {
	const params = ctx.request.query
    // 查询歌单列表
	const result = await callCloudFn(ctx, 'music', {
        $url: 'playlist',
        start: parseInt(params.start),
        count: parseInt(params.count)
    })
    ctx.body = {
        code: 20000,
        data: result.data
    }
})

router.get('/getById', async(ctx, next) => {
    const query = `db.collection('playlist').doc('${ctx.request.query.id}').get()`
    const result = await callCloudDB(ctx, 'databasequery', query)
    ctx.body = {
        code: 20000,
        data: result.data
    }
})

router.post('/updatePlaylist', async(ctx, next) => {
    const params = ctx.request.body
    const query = `db.collection('playlist').doc('${params._id}').update({
        data: {
            name: '${params.name}',
            copywriter: '${params.copywriter}'
        }
    })`
    const result = await callCloudDB(ctx, 'databaseupdate', query)
    ctx.body = {
        code: 20000,
        data: result.data
    }
})

router.post('/del', async(ctx, next) => {
    const params = ctx.request.body
    const query = `db.collection('playlist').doc('${params.id}').remove()`
    const result = await callCloudDB(ctx, 'databasedelete', query)
    ctx.body = {
        code: 20000,
        data: result.data
    }   
})

module.exports = router
